/**
 * 对象数组去重
 * @param { Array } arr [{id:1},{id:1},{id:2}]
 * @param { String } prop 'id'
 * @return { Array } [{id:1},{id:2}]
 */
export function filterRepeat(arr,prop){
    return arr.filter(function(element,index,self){
      return self.findIndex(el=>el[prop]==element[prop])===index
    })
}
/**
 * 数组去重
 * @param { Array } 
 * @return { Array } 
 */
export function unique (arr) {
  return Array.from(new Set(arr))
}

/**
 * 请求文件的ajax
 * @param { String } 
 * @return { String } 
 */
export function doAjaxCall (the_request){
  return new Promise((resolve, reject) =>{
    var request = null;
    if(window.XMLHttpRequest){
      request = new XMLHttpRequest();
    }else if( window.ActiveXObject ){
      request=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(request){
      request.open("GET",the_request,true);
      request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      request.onreadystatechange = function(){
        if(request.readyState === 4)
        {
          if (request.status == 200 || request.status == 0)
          {
          resolve(request.responseText)
          }
        }
      }
      request.send(null);
    } else {
      reject('error')
    }
  })
}

/**
 * 动态加载script
 */
export function loadScriptAsync(src) {
  return new Promise((resolve, reject) => {
    const bodyNode = document.getElementsByTagName('body')[0]
    const scriptNode = document.createElement('script')
    scriptNode.type = 'text/javascript'
    scriptNode.src = src
    bodyNode.appendChild(scriptNode)
    bodyNode.onload = event => {
      console.log("成功了")
      resolve(event)
      
    }
    bodyNode.onerror = event => {
      console.log("失败了",event)
      reject(event)
    }
  })
}
